package com.zp.framework.services.test;

import javax.ws.rs.core.MediaType;

import junit.framework.Assert;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;

import com.sun.jersey.api.client.Client;
import com.sun.jersey.api.client.ClientResponse;
import com.sun.jersey.api.client.UniformInterfaceException;
import com.sun.jersey.api.client.WebResource;
import com.sun.jersey.api.client.config.ClientConfig;
import com.sun.jersey.api.client.config.DefaultClientConfig;
import com.zp.bean.common.ResultCommonVo;
import com.zp.bean.user.ResultPermissionVo;
import com.zp.domain.Permission;
import com.zp.util.Constant;

/**
 * test permission services function with Junit
 * 
 * @author chenlijun08@gmail.com
 * 
 * @version 1.0
 * 
 * @date 2014/2/16
 * 
 */

@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(locations = { "classpath:configure/applicationContext.xml" })
public class RunPermissionServices {

	private static final Log logger = LogFactory
			.getLog(RunPermissionServices.class);
	private static final String URI = "http://localhost:8082/ZP";
	private WebResource service;

	@Before
	public void init() {
		ClientConfig config = new DefaultClientConfig();
		Client client = Client.create(config);
		service = client.resource(URI);
		logger.info("*********** testing to initial services client ****************");
	}

	@After
	public void destory() {
		service = null;
		logger.info("*********** testing to destory services instance ******************");
	}

	/** @Test **/
	public void testAddPermission() {
		try {
			Permission p = new Permission();
			p.setName("获取用户角色");
			p.setCode("P00009");
			p.setStatus("1");
			p.setTypeCode("AU0001");
			p.setParentCode("P00001");
			p.setUrl("/user/getUserRole");
			ClientResponse response = service.path("/permission/addPermission")
					.type(MediaType.APPLICATION_JSON_TYPE)
					.post(ClientResponse.class, p);
			Assert.assertEquals(Constant.OK,
					response.getEntity(ResultCommonVo.class).getMessage());
		} catch (UniformInterfaceException e) {
			System.out.println(e.getResponse().getStatus());
			if (e.getResponse().getStatus() == 404) {
			} else if (e.getResponse().getStatus() == 401) {
				throw new IllegalStateException(e);
			} else if (e.getResponse().getStatus() == 403) {
				throw new IllegalStateException(e);
			} else {
				throw e;
			}
		}

	}

	/** @Test **/
	public void findPermissionById() {
		ClientResponse response = service.path("/permission/getPermission")
				.queryParam("permissionId", "402881e6445916b401445921eff30003")
				.type(MediaType.APPLICATION_JSON_TYPE)
				.get(ClientResponse.class);
		Assert.assertEquals("AU0001",
				response.getEntity(ResultPermissionVo.class).getPermission()
						.getType().getCode());

	}

	@Test
	public void testSearchPermission() {
		ClientResponse response = service.path("/permission/searchPermission")
				.queryParam("pageNo", "1").queryParam("pageSize", "10")
				.type(MediaType.APPLICATION_JSON_TYPE)
				.get(ClientResponse.class);
		ResultPermissionVo result = response
				.getEntity(ResultPermissionVo.class);
		Assert.assertEquals(9, result.getTotalCount());
	}

}
